//编辑车辆

interface ICarManagentEditViewModel extends ng.IScope {
    /**
     * 车牌号
     */
    carCode: string;
    /**
     * 车型
     */
    carType: string;
    /**
     * 车长
     */
    carLength: string;
    /**
     * 驾驶员
     */
    driver: string;
    /**
     * 驾驶员手机号
     */
    driverPhone: string;
    /**
     * 承运方式
     */
    carrierCategory: string;
    /**
     * 核定载重
     */
    bearing: string;
    /**
     * 车辆来源
     */
    carSource: string;
    /**
     * 道路运输许可证号
     */
    permitNumber: string;
    /**
    * 所属辖区
    */
    countrySubdivisionCode: string;
    /**
    * 道路运输证字号
    */
    roadTransportCertificateNumber: string;
    /**
    * 省编号
    */
    provinceCode: string;
    /**
     * 市编号
     */
    cityCode: string;
    /**
     * 区编号
     */
    areaCode: string;
    /**
     * 地址
     */
    address: string;
    /**
     * 身份证号码
     */
    identity:string;
    /**
     * 车长下拉
     */
    carLengthDropDown: ValueListItemResponse[];
    /**
     * 车型下拉
     */
    carTypeDropDown: ValueListItemResponse[];
    /**
     * 承运类别下拉
     */
    carrierCategoryDropDown: ValueListItemResponse[];
     /**
     * 所属辖区市列表
     */
    CityList: any;
    /**
     * 所属辖区区列表
     */
    AreaList: any;
    /**
     * 保存事件
     */
    click: () => void;

    /**
     * 返回事件
     */
    backUrl:()=>void;
}

class carEditControllert {
    carId: string;

    constructor(private $scope: ICarManagentEditViewModel, private carService: ICarService, private valueService: IValueService,
        private $state: angular.ui.IStateService, private $location: ng.ILocationService, private $ngBootbox: BootboxService, private $timeout: ng.ITimeoutService, private areaService: IAreaService) {
        this.init();
    }

    init(): void {
        this.clearInput();

        this.carId = this.$location.search().id;
        if (!this.carId) {
            this.carId = JSON.parse(window.localStorage.getItem("car").toString()).carId;
        } else {
            this.carId = this.carId;
        }

        let car = {
            carId: this.carId,
        }
        window.localStorage.setItem("car", JSON.stringify(car));

        // this.$scope.click = this.saveCar;
        this.loadCarLengthDropDown();
        this.loadCarTypeDropDown();
        this.loadCarrierCategoryDropDown();
        this.loadData();
        this.$timeout(() => {
            $(".province").attr("disabled", "disabled");
            $(".city").attr("disabled", "disabled");
            $(".area").attr("disabled", "disabled");
        })

        this.$scope.backUrl=this.backUrl;
    }

    loadCarLengthDropDown(): void {
        this.$scope.carLengthDropDown = this.valueService.getCarLengthList().data;
    }

    loadCarTypeDropDown(): void {
        this.$scope.carTypeDropDown = this.valueService.getCarTypeList().data;
    }

    loadCarrierCategoryDropDown(): void {
        this.$scope.carrierCategoryDropDown = this.valueService.getCarriageTypeList().data;
    }

    /**
    * 获取市区下拉
    */
    getProvinceList = () => {
        this.areaService.getCity(this.$scope.provinceCode).then(res => {
            this.$scope.CityList = res.data.data;

        });
        this.areaService.getCounty(this.$scope.cityCode).then(res => {
            this.$scope.AreaList = res.data.data;
        });
    }

    loadData = () => {

        this.carService.getDetail(this.carId).then(result => {
            let car = {
                carId: this.carId,
                carCode: result.data.carCode,
                average: result.data.average,//总分
                praiseCount: result.data.praiseCount//好评数量
            }
            window.localStorage.setItem("car", JSON.stringify(car));

            this.$scope.carCode = result.data.carCode;
            this.$scope.carType = result.data.carType;
            this.$scope.carLength = result.data.carLength;
            this.$scope.driver = result.data.driver;
            this.$scope.driverPhone = result.data.driverPhone;
            this.$scope.carrierCategory = result.data.carriageType;
            this.$scope.bearing = result.data.bearing;
            this.$scope.carSource = result.data.carSource;
            this.$scope.countrySubdivisionCode = result.data.countrySubdivisionCode;
            let countrySubdinvisionCode = this.$scope.countrySubdivisionCode;
            if (this.$scope.countrySubdivisionCode != undefined && this.$scope.countrySubdivisionCode != "") {
                this.$scope.provinceCode = countrySubdinvisionCode.substring(0, 2);
                this.$scope.cityCode = countrySubdinvisionCode.substring(0, 4);
                this.$scope.areaCode = countrySubdinvisionCode.substring(0, 6);
                this.getProvinceList();
            }
            this.$scope.permitNumber = result.data.permitNumber;
            this.$scope.roadTransportCertificateNumber = result.data.roadTransportCertificateNumber;
            this.$scope.address = result.data.address;
            this.$scope.identity = result.data.identity;
        });
    }

    saveCar = () => {
        if (!$("#form").valid()) { return };
        this.$ngBootbox.confirm("确定保存该数据吗？").then(e => {
            this.carService.edit(
                this.carId,
                this.$scope.carCode.toUpperCase(),
                this.$scope.carType,
                this.$scope.carLength,
                this.$scope.driver,
                this.$scope.driverPhone,
                this.$scope.carrierCategory,
                parseInt(this.$scope.bearing), //这部分要从获取接口调整类型
                this.$scope.carSource,
                this.$scope.permitNumber,
                this.$scope.countrySubdivisionCode,
                this.$scope.roadTransportCertificateNumber,
                this.$scope.address,
                this.$scope.identity.toUpperCase()
            ).then(result => {
                this.clearInput();
                this.$state.go("app.car.carAttachmentEdit", { carId: this.carId });
            });
        });
    }

    clearInput() {
        this.$scope.carCode = "";
        this.$scope.carType = "";
        this.$scope.carLength = "";
        this.$scope.driver = "";
        this.$scope.driverPhone = "";
        this.$scope.carrierCategory = "";
        this.$scope.bearing = "";
        this.$scope.carSource = "";
        this.$scope.permitNumber = "";
        this.$scope.countrySubdivisionCode = "";
        this.$scope.roadTransportCertificateNumber = "";
        this.$scope.address = "";
        this.$scope.identity = "";
        this.$scope.carLengthDropDown = [];
        this.$scope.carTypeDropDown = [];
        this.$scope.carTypeDropDown = [];
    }

    backUrl=()=>{
        this.$state.go('app.car.carManagent',{ name:'carManagent' })
    }

}

angular.module("tms").controller("carEditControllert", ["$scope", "carService", "valueService", "$state", "$location", "$ngBootbox", "$timeout", "areaService", carEditControllert]);